package com.hskn.hss.module.totalcost.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.module.inspectionCfg.entity.TlInspectionCfg;
import com.hskn.hss.module.inspectionCfg.mapper.TlInspectionCfgMapper;
import com.hskn.hss.module.insuranceCfg.entity.TlInsuranceCfg;
import com.hskn.hss.module.insuranceCfg.mapper.TlInsuranceCfgMapper;
import com.hskn.hss.module.maintaincfg.entity.TlMaintainCfg;
import com.hskn.hss.module.maintaincfg.mapper.TlMaintainCfgMapper;
import com.hskn.hss.module.othercarcsotCfg.entity.TlOthercarcostCfg;
import com.hskn.hss.module.othercarcsotCfg.mapper.TlOthercarcostCfgMapper;
import com.hskn.hss.module.tlcar.entity.TlCar;
import com.hskn.hss.module.tlcar.service.ITlCarService;
import com.hskn.hss.module.tlcostoperate.entity.TlCostOperate;
import com.hskn.hss.module.tlcostoperate.mapper.TlCostOperateMapper;
import com.hskn.hss.module.tlempincidental.entity.TlEmpIncidental;
import com.hskn.hss.module.tlempincidental.mapper.TlEmpIncidentalMapper;
import com.hskn.hss.module.tlempsalary.entity.TlEmpSalary;
import com.hskn.hss.module.tlempsalary.mapper.TlEmpSalaryMapper;
import com.hskn.hss.module.tloilcostcfg.entity.TlOilcostCfg;
import com.hskn.hss.module.tloilcostcfg.mapper.TlOilcostCfgMapper;
import com.hskn.hss.module.tlrepaircfg.entity.TlRepairCfg;
import com.hskn.hss.module.tlrepaircfg.mapper.TlRepairCfgMapper;
import com.hskn.hss.module.totalcost.dto.SummaryVehicleCostDTO;
import com.hskn.hss.module.totalcost.entity.totalcost;
import com.hskn.hss.module.totalcost.service.TotalCostService;
import com.hskn.hss.module.totalcost.to.SummaryOperatingExpensesTO;
import com.hskn.hss.module.totalcost.to.SummaryPersonnelCostsTO;
import com.hskn.hss.module.totalcost.to.SummaryTotalCostTO;
import com.hskn.hss.module.totalcost.to.SummaryVehicleCostTO;
import com.hskn.hss.module.totalcost.vo.CarCostVO;
import com.hskn.hss.module.totalcost.vo.EmpCostVO;
import com.hskn.hss.module.totalcost.vo.OperateCostVO;
import com.hskn.hss.module.totalcost.vo.SummaryTotalCostVO;
import com.hskn.hss.module.violationCfg.entity.TlViolationCfg;
import com.hskn.hss.module.violationCfg.mapper.TlViolationCfgMapper;
import com.hskn.hss.utils.BTable;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-15
 */
@Service
public class TotalCostServiceImpl implements TotalCostService {
    @Resource
    TlCostOperateMapper costOperateMapper;
    @Resource
    ISysDepartService departService;
    @Resource
    TlEmpSalaryMapper empSalaryMapper;
    @Resource
    TlEmpIncidentalMapper empIncidentalMapper;
    @Resource
    ITlCarService carService;
    @Resource
    TlOilcostCfgMapper oilcostCfgMapper;
    @Resource
    TlRepairCfgMapper repairCfgMapper;
    @Resource
    TlMaintainCfgMapper maintainCfgMapper;
    @Resource
    TlInsuranceCfgMapper insuranceCfgMapper;
    @Resource
    TlInspectionCfgMapper inspectionCfgMapper;
    @Resource
    TlViolationCfgMapper violationCfgMapper;
    @Resource
    TlOthercarcostCfgMapper othercarcostCfgMapper;

    /**
     * 运营杂费汇总Echars图
     *
     * @param summaryOperatingExpensesTO
     * @return java.util.List<java.util.List < java.lang.Object>>
     * @author zcy
     * @date 2021/10/25 14:43
     */
    @Override
    public List<List<Object>> getOperateChars(SimpleDateFormat formatter, List<TlCostOperate> costOperateList, SummaryOperatingExpensesTO summaryOperatingExpensesTO) throws ParseException {
        List<List<Object>> resultList = new ArrayList<>();
        List<Object> res = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        dateList = Tools.getMonthList(formatter, summaryOperatingExpensesTO.getStartDate(), summaryOperatingExpensesTO.getEndDate());
        for (String date : dateList) {
            res.add(date);
            resultList.add(res);
            res = new ArrayList<>();
        }
        Map<String, List<TlCostOperate>> costOperateMap = GroupingByUtils.getGroupingForMapList(costOperateList, TlCostOperate::getPayTimeString);
        List<BTable> bTableList = new ArrayList<>();
        List<BTable> waterBTableList = new ArrayList<>();
        List<BTable> electricBTableList = new ArrayList<>();
        List<BTable> otherBTableList = new ArrayList<>();
        for (String time : costOperateMap.keySet()) {
            bTableList.add(buildCostOperateBTable(time, costOperateMap.get(time)));
            if (CollectionUtils.isNotEmpty(costOperateMap.get(time))) {
                Map<String, List<TlCostOperate>> typeMap = GroupingByUtils.getGroupingForMapList(costOperateMap.get(time), TlCostOperate::getCosttype);
                for (String costType : typeMap.keySet()) {
                    if (costType.equals("水费")) {
                        waterBTableList.add(buildCostOperateBTable(time, typeMap.get(costType)));
                    }
                    if (costType.equals("电费")) {
                        electricBTableList.add(buildCostOperateBTable(time, typeMap.get(costType)));
                    }
                    if (costType.equals("杂费")) {
                        otherBTableList.add(buildCostOperateBTable(time, typeMap.get(costType)));
                    }
                }
            }
        }
        resultList = Tools.getBTable(resultList, bTableList);
        resultList = Tools.getBTable(resultList, waterBTableList);
        resultList = Tools.getBTable(resultList, electricBTableList);
        resultList = Tools.getBTable(resultList, otherBTableList);
        return resultList;
    }

    /**
     * 运营杂费汇总查询
     *
     * @param summaryOperatingExpensesTO
     * @return java.util.List<java.util.List < java.lang.Object>>
     * @author zcy
     * @date 2021/10/25 14:43
     */
    @Override
    @DataScope(deptAlias = "waterdept", userAlias = "creatBy")
    public List<TlCostOperate> getTlCostOperateList(SummaryOperatingExpensesTO summaryOperatingExpensesTO, SimpleDateFormat formatter) {
        QueryWrapper<TlCostOperate> costOperateQueryWrapper = new QueryWrapper<>();
        if (summaryOperatingExpensesTO.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(summaryOperatingExpensesTO.getParams().get("dataScope").toString())) {
            costOperateQueryWrapper.lambda().apply(summaryOperatingExpensesTO.getParams().get("dataScope").toString());
        }
        if (StringUtils.isNotEmpty(summaryOperatingExpensesTO.getDepId())) {
            costOperateQueryWrapper.lambda().eq(TlCostOperate::getWaterdept, summaryOperatingExpensesTO.getDepId());
        }
        if (StringUtils.isNotEmpty(summaryOperatingExpensesTO.getDepTypeName())) {
            QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
            departQueryWrapper.lambda().eq(SysDepart::getOrgType, summaryOperatingExpensesTO.getDepTypeName());
            Map<String, SysDepart> departMap = departService.getDepartMap(departQueryWrapper);
            costOperateQueryWrapper.lambda().in(TlCostOperate::getWaterdept, departMap.keySet());
        }
        if (StringUtils.isNotEmpty(summaryOperatingExpensesTO.getCostType())) {
            costOperateQueryWrapper.lambda().eq(TlCostOperate::getCosttype, summaryOperatingExpensesTO.getCostType());
        }
        if (Objects.nonNull(summaryOperatingExpensesTO.getStartDate()) && Objects.nonNull(summaryOperatingExpensesTO.getEndDate())) {
            costOperateQueryWrapper.lambda().between(TlCostOperate::getPaytime, summaryOperatingExpensesTO.getStartDate(), summaryOperatingExpensesTO.getEndDate());
        }
        List<TlCostOperate> costOperateList = costOperateMapper.selectList(costOperateQueryWrapper);
        for (TlCostOperate costOperate : costOperateList) {
            costOperate.setPayTimeString(formatter.format(costOperate.getPaytime()));
        }
        return costOperateList;
    }

    /**
     * 运营杂费汇总Echars图构造
     *
     * @param time
     * @param costOperateList
     * @return com.hskn.hss.utils.BTable
     * @author zcy
     * @date 2021/10/25 14:45
     */
    private BTable buildCostOperateBTable(String time, List<TlCostOperate> costOperateList) {
        BTable bTable = new BTable();
        if (null == costOperateList || (Objects.nonNull(costOperateList) && costOperateList.isEmpty())) {
            return bTable;
        }
        BigDecimal sum = BigDecimal.ZERO;
        String name = "";
        for (TlCostOperate tlCostOperate : costOperateList) {
            sum = sum.add(tlCostOperate.getTotalcost());
            name += tlCostOperate.getPayTimeString() + "##";
        }
        bTable.setTime(time);
        if (StringUtils.isNotEmpty(name)) {
            bTable.setName(name.substring(0, name.lastIndexOf("##")));
        }
        bTable.setData(sum);
        return bTable;
    }

    /**
     * 运营杂费汇总Echars图构造
     *
     * @param time
     * @param costOperateList
     * @return com.hskn.hss.utils.BTable
     * @author zcy
     * @date 2021/10/25 14:45
     */
    private BTable buildCostOperateBTable(String time, String typeName, List<TlCostOperate> costOperateList) {
        BTable bTable = new BTable();
        if (null == costOperateList || (Objects.nonNull(costOperateList) && costOperateList.isEmpty())) {
            return bTable;
        }
        BigDecimal sum = BigDecimal.ZERO;
        String name = "";
        for (TlCostOperate tlCostOperate : costOperateList) {
            sum = sum.add(tlCostOperate.getTotalcost());
            name += tlCostOperate.getPayTimeString() + "##";
        }
        bTable.setTime(time);
        bTable.setCarTypeName(typeName);
        if (StringUtils.isNotEmpty(name)) {
            bTable.setName(name.substring(0, name.lastIndexOf("##")));
        }
        bTable.setData(sum);
        return bTable;
    }

    /**
     * 运营杂费汇总列表
     *
     * @param summaryOperatingExpensesTO
     * @return java.util.List<com.hskn.hss.module.totalcost.vo.OperateCostVO>
     * @author zcy
     * @date 2021/10/25 14:46
     */
    @Override
    public List<OperateCostVO> getopeList(SimpleDateFormat formatter, List<TlCostOperate> costOperateList, SummaryOperatingExpensesTO summaryOperatingExpensesTO) throws HssException, ParseException {
        if (costOperateList.isEmpty()) {
            return new ArrayList<>();
        }
        Map<String, List<TlCostOperate>> costOperateMap = GroupingByUtils.getGroupingForMapList(costOperateList, TlCostOperate::getWaterdept);
        QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
        departQueryWrapper.lambda().in(SysDepart::getId, costOperateMap.keySet());
        Map<String, SysDepart> departMap = departService.getDepartMap(departQueryWrapper);
        List<OperateCostVO> operateCostVOList = new ArrayList<>();
        for (String depId : costOperateMap.keySet()) {
            if (departMap.containsKey(depId)) {
                operateCostVOList.add(buildOperateCostVO(departMap.get(depId), costOperateMap.get(depId)));
            }
        }
        return operateCostVOList;
    }

    /**
     * 运营杂费汇总列表构造
     *
     * @param depart
     * @param operateList
     * @return java.util.List<com.hskn.hss.module.totalcost.vo.OperateCostVO>
     * @author zcy
     * @date 2021/10/25 14:46
     */
    private OperateCostVO buildOperateCostVO(SysDepart depart, List<TlCostOperate> operateList) {
        operateList = operateList.stream().sorted(Comparator.comparing(TlCostOperate::getPayTimeString).reversed()).collect(Collectors.toList());
        OperateCostVO operateCostVO = new OperateCostVO();
        if (operateList == null || (operateList != null && operateList.isEmpty())) {
            return operateCostVO;
        }
        BigDecimal allCostNum = BigDecimal.ZERO;
        BigDecimal waterCostNum = BigDecimal.ZERO;
        BigDecimal electricCostNum = BigDecimal.ZERO;
        BigDecimal otherCostNum = BigDecimal.ZERO;
        for (TlCostOperate costOperate : operateList) {
            allCostNum = allCostNum.add(costOperate.getTotalcost());
            if (StringUtils.isNotEmpty(costOperate.getCosttype()) && costOperate.getCosttype().equals("水费")) {
                waterCostNum = waterCostNum.add(costOperate.getTotalcost());
            }
            if (StringUtils.isNotEmpty(costOperate.getCosttype()) && costOperate.getCosttype().equals("电费")) {
                electricCostNum = electricCostNum.add(costOperate.getTotalcost());
            }
            if (StringUtils.isNotEmpty(costOperate.getCosttype()) && costOperate.getCosttype().equals("杂费")) {
                otherCostNum = otherCostNum.add(costOperate.getTotalcost());
            }
        }

        operateCostVO.setDepName(depart.getDepartName());
        operateCostVO.setDepTypeName(depart.getOrgType());
        if (operateList.size() > 1) {
            operateCostVO.setMonthOfYear(operateList.get(operateList.size() - 1).getPayTimeString() + "-" + operateList.get(0).getPayTimeString());
        } else {
            operateCostVO.setMonthOfYear(operateList.get(0).getPayTimeString());
        }
        operateCostVO.setAllCostNum(allCostNum);
        operateCostVO.setWaterCostNum(waterCostNum);
        operateCostVO.setElectricCostNum(electricCostNum);
        operateCostVO.setOtherCostNum(otherCostNum);
        operateCostVO.setAllCostNum(allCostNum);
        return operateCostVO;
    }

    /**
     * 人员费用汇总Echars图
     *
     * @param summaryPersonnelCostsTO
     * @return java.util.List<java.util.List < java.lang.Object>>
     * @author zcy
     * @date 2021/10/25 16:22
     */
    @Override
    public List<List<Object>> getempChars(SimpleDateFormat formatter, List<TlEmpSalary> empSalaryList, List<TlEmpIncidental> empIncidentalList, SummaryPersonnelCostsTO summaryPersonnelCostsTO) throws ParseException {
        List<List<Object>> resultList = new ArrayList<>();
        List<Object> res = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        dateList = Tools.getMonthList(formatter, summaryPersonnelCostsTO.getStartDate(), summaryPersonnelCostsTO.getEndDate());
        for (String date : dateList) {
            res.add(date);
            resultList.add(res);
            res = new ArrayList<>();
        }
        Map<String, List<TlEmpSalary>> empSalaryMap = GroupingByUtils.getGroupingForMapList(empSalaryList, TlEmpSalary::getMonth);
        Map<String, List<TlEmpIncidental>> empIncidentalMap = GroupingByUtils.getGroupingForMapList(empIncidentalList, TlEmpIncidental::getMonth);
        List<BTable> bTableList = new ArrayList<>();
        List<BTable> empSalaryBTableList = new ArrayList<>();
        List<BTable> empIncidentalBTableList = new ArrayList<>();
        for (String empSalaryMonth : empSalaryMap.keySet()) {
            bTableList.add(buildEmpCostBTable(empSalaryMonth, empSalaryMap.get(empSalaryMonth)));
            empSalaryBTableList.add(buildEmpCostBTable(empSalaryMonth, empSalaryMap.get(empSalaryMonth)));
        }
        for (String empIncidentalMonth : empIncidentalMap.keySet()) {
            bTableList.add(buildEmpCostBTable1(empIncidentalMonth, empIncidentalMap.get(empIncidentalMonth)));
            empIncidentalBTableList.add(buildEmpCostBTable1(empIncidentalMonth, empIncidentalMap.get(empIncidentalMonth)));
        }
        Map<String, List<BTable>> bTableMap = GroupingByUtils.getGroupingForMapList(bTableList, BTable::getTime);
        List<BTable> allBTableList = new ArrayList<>();
        for (String time : bTableMap.keySet()) {
            allBTableList.add(buildBTable(time, bTableMap.get(time)));
        }
        resultList = Tools.getBTable(resultList, allBTableList);
        resultList = Tools.getBTable(resultList, empSalaryBTableList);
        resultList = Tools.getBTable(resultList, empIncidentalBTableList);
        return resultList;
    }

    private BTable buildBTable(String time, List<BTable> bTableList) {
        BTable bTable = new BTable();
        BigDecimal sum = BigDecimal.ZERO;
        for (BTable table : bTableList) {
            sum = sum.add(new BigDecimal(table.getData().toString()));
        }
        bTable.setTime(time);
        bTable.setData(sum);
        return bTable;
    }

    /**
     * 人员费用汇总Echars图构造
     *
     * @param month
     * @param empIncidentalList
     * @return java.util.List<java.util.List < java.lang.Object>>
     * @author zcy
     * @date 2021/10/25 16:22
     */
    private BTable buildEmpCostBTable1(String month, List<TlEmpIncidental> empIncidentalList) {
        BTable bTable = new BTable();
        String name = "";
        BigDecimal allCostSum = BigDecimal.ZERO;
        for (TlEmpIncidental tlEmpIncidental : empIncidentalList) {
            allCostSum = allCostSum.add(tlEmpIncidental.getPaycost());
            if (StringUtils.isNotEmpty(tlEmpIncidental.getMonth())) {
                name += tlEmpIncidental.getMonth() + "##";
            }
        }
        bTable.setTime(month);
        if (StringUtils.isNotEmpty(name)) {
            bTable.setName(name.substring(0, name.lastIndexOf("##")));
        }
        bTable.setData(allCostSum);
        return bTable;
    }

    /**
     * 人员费用汇总Echars图构造
     *
     * @param month
     * @param empSalaryList
     * @return java.util.List<java.util.List < java.lang.Object>>
     * @author zcy
     * @date 2021/10/25 16:22
     */
    private BTable buildEmpCostBTable(String month, List<TlEmpSalary> empSalaryList) {
        BTable bTable = new BTable();
        String name = "";
        BigDecimal allCostSum = BigDecimal.ZERO;
        for (TlEmpSalary empSalary : empSalaryList) {
            //基本工资
            allCostSum = allCostSum.add(empSalary.getBasicSalary() == null ? new BigDecimal(0) : empSalary.getBasicSalary());
            //岗位工资
            allCostSum = allCostSum.add(empSalary.getPostSalary() == null ? new BigDecimal(0) : empSalary.getPostSalary());
            //绩效工资
            allCostSum = allCostSum.add(empSalary.getMeritSalary() == null ? new BigDecimal(0) : empSalary.getMeritSalary());
            //补贴
            allCostSum = allCostSum.add(empSalary.getSubsidy() == null ? new BigDecimal(0) : empSalary.getSubsidy());
            //社保
            allCostSum = allCostSum.add(empSalary.getSocialSecurity() == null ? new BigDecimal(0) : empSalary.getSocialSecurity());
            //公积金
            allCostSum = allCostSum.add(empSalary.getProvidentFund() == null ? new BigDecimal(0) : empSalary.getProvidentFund());
            //个税
            allCostSum = allCostSum.subtract(empSalary.getIncomeTax() == null ? new BigDecimal(0) : empSalary.getIncomeTax());
            //扣款
            allCostSum = allCostSum.subtract(empSalary.getDeduction() == null ? new BigDecimal(0) : empSalary.getDeduction());

            if (StringUtils.isNotEmpty(empSalary.getMonth())) {
                name += empSalary.getMonth() + "##";

            }
        }
        bTable.setTime(month);
        if (StringUtils.isNotEmpty(name)) {
            bTable.setName(name.substring(0, name.lastIndexOf("##")));
        }
        bTable.setData(allCostSum);
        return bTable;
    }

    /**
     * 人员费用汇总Echars图构造
     *
     * @param month
     * @param empSalaryList
     * @param empIncidentalList
     * @return java.util.List<java.util.List < java.lang.Object>>
     * @author zcy
     * @date 2021/10/25 16:22
     */
    private BTable buildEmpCostBTable(String month, String typeName, List<TlEmpSalary> empSalaryList, List<TlEmpIncidental> empIncidentalList) {
        BTable bTable = new BTable();
        if (empSalaryList.isEmpty() && empIncidentalList.isEmpty()) {
            return bTable;
        }
        String name = "";
        BigDecimal allCostSum = BigDecimal.ZERO;
        for (TlEmpSalary empSalary : empSalaryList) {
            //基本工资
            allCostSum = allCostSum.add(empSalary.getBasicSalary());
            //岗位工资
            allCostSum = allCostSum.add(empSalary.getPostSalary());
            //绩效工资
            allCostSum = allCostSum.add(empSalary.getMeritSalary());
            //补贴
            allCostSum = allCostSum.add(empSalary.getSubsidy());
            //社保
            allCostSum = allCostSum.add(empSalary.getSocialSecurity());
            //公积金
            allCostSum = allCostSum.add(empSalary.getProvidentFund());
            //个税
            allCostSum = allCostSum.subtract(empSalary.getIncomeTax());
            //扣款
            allCostSum = allCostSum.subtract(empSalary.getDeduction());

            if (StringUtils.isNotEmpty(empSalary.getMonth())) {
                name += empSalary.getMonth() + "##";

            }
        }
        for (TlEmpIncidental tlEmpIncidental : empIncidentalList) {
            allCostSum = allCostSum.add(tlEmpIncidental.getPaycost());
            if (StringUtils.isNotEmpty(tlEmpIncidental.getMonth())) {
                name += tlEmpIncidental.getMonth() + "##";
            }
        }
        bTable.setTime(month);
        if (StringUtils.isNotEmpty(name)) {
            bTable.setName(name.substring(0, name.lastIndexOf("##")));
        }
        bTable.setCarTypeName(typeName);
        bTable.setData(allCostSum);
        return bTable;
    }

    /**
     * 人员工资组成汇总查询
     *
     * @param summaryPersonnelCostsTO
     * @return java.util.List<java.util.List < java.lang.Object>>
     * @author zcy
     * @date 2021/10/25 14:43
     */
    @Override
    @DataScope(deptAlias = "empdeptid", userAlias = "creatBy")
    public List<TlEmpSalary> getTlEmpSalaryList(SummaryPersonnelCostsTO summaryPersonnelCostsTO) {
        QueryWrapper<TlEmpSalary> empSalaryQueryWrapper = new QueryWrapper<>();
        if (summaryPersonnelCostsTO.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(summaryPersonnelCostsTO.getParams().get("dataScope").toString())) {
            empSalaryQueryWrapper.lambda().apply(summaryPersonnelCostsTO.getParams().get("dataScope").toString());
        }
        if (StringUtils.isNotEmpty(summaryPersonnelCostsTO.getDepId())) {
            empSalaryQueryWrapper.lambda().eq(TlEmpSalary::getEmpdeptid, summaryPersonnelCostsTO.getDepId());
        }
        if (StringUtils.isNotEmpty(summaryPersonnelCostsTO.getDepTypeName())) {
            QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
            departQueryWrapper.lambda().eq(SysDepart::getOrgType, summaryPersonnelCostsTO.getDepTypeName());
            Map<String, SysDepart> departMap = departService.getDepartMap(departQueryWrapper);
            empSalaryQueryWrapper.lambda().in(TlEmpSalary::getEmpdeptid, departMap.keySet());
        }
        if (StringUtils.isNotEmpty(summaryPersonnelCostsTO.getWorkType())) {
            empSalaryQueryWrapper.lambda().eq(TlEmpSalary::getWorkType, summaryPersonnelCostsTO.getWorkType());
        }
        if (Objects.nonNull(summaryPersonnelCostsTO.getStartDate()) && Objects.nonNull(summaryPersonnelCostsTO.getEndDate())) {
            empSalaryQueryWrapper.lambda().between(TlEmpSalary::getPaydate, summaryPersonnelCostsTO.getStartDate(), summaryPersonnelCostsTO.getEndDate());
        }
        empSalaryQueryWrapper.lambda().orderByDesc(TlEmpSalary::getMonth);
        List<TlEmpSalary> empSalaryList = empSalaryMapper.selectList(empSalaryQueryWrapper);
        return empSalaryList;
    }

    /**
     * 人员杂费汇总查询
     *
     * @param summaryPersonnelCostsTO
     * @return java.util.List<java.util.List < java.lang.Object>>
     * @author zcy
     * @date 2021/10/25 14:43
     */
    @Override
    @DataScope(deptAlias = "empdeptid", userAlias = "creatBy")
    public List<TlEmpIncidental> getTlEmpIncidentalList(SummaryPersonnelCostsTO summaryPersonnelCostsTO) {
        QueryWrapper<TlEmpIncidental> empIncidentalQueryWrapper = new QueryWrapper<>();
        if (summaryPersonnelCostsTO.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(summaryPersonnelCostsTO.getParams().get("dataScope").toString())) {
            empIncidentalQueryWrapper.lambda().apply(summaryPersonnelCostsTO.getParams().get("dataScope").toString());
        }
        if (StringUtils.isNotEmpty(summaryPersonnelCostsTO.getDepId())) {
            empIncidentalQueryWrapper.lambda().eq(TlEmpIncidental::getEmpdeptid, summaryPersonnelCostsTO.getDepId());
        }
        if (StringUtils.isNotEmpty(summaryPersonnelCostsTO.getWorkType())) {
            empIncidentalQueryWrapper.lambda().eq(TlEmpIncidental::getWorkType, summaryPersonnelCostsTO.getWorkType());
        }
        if (StringUtils.isNotEmpty(summaryPersonnelCostsTO.getDepTypeName())) {
            QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
            departQueryWrapper.lambda().eq(SysDepart::getOrgType, summaryPersonnelCostsTO.getDepTypeName());
            Map<String, SysDepart> departMap = departService.getDepartMap(departQueryWrapper);
            empIncidentalQueryWrapper.lambda().in(TlEmpIncidental::getEmpdeptid, departMap.keySet());
        }
        if (Objects.nonNull(summaryPersonnelCostsTO.getStartDate()) && Objects.nonNull(summaryPersonnelCostsTO.getEndDate())) {
            empIncidentalQueryWrapper.lambda().between(TlEmpIncidental::getPaydate, summaryPersonnelCostsTO.getStartDate(), summaryPersonnelCostsTO.getEndDate());
        }
        empIncidentalQueryWrapper.lambda().orderByDesc(TlEmpIncidental::getMonth);
        List<TlEmpIncidental> empIncidentalList = empIncidentalMapper.selectList(empIncidentalQueryWrapper);
        return empIncidentalList;
    }

    private BigDecimal isBigDecimalNull(BigDecimal v) {
        if (v != null) {
            return v.setScale(2);
        }
        return BigDecimal.ZERO.setScale(2);
    }

    /**
     * 人员杂费汇总列表
     *
     * @param summaryPersonnelCostsTO
     * @return java.util.List<com.hskn.hss.module.totalcost.vo.EmpCostVO>
     * @author zcy
     * @date 2021/10/26 9:04
     */
    @Override
    public List<EmpCostVO> getempList(List<TlEmpSalary> empSalaryList, List<TlEmpIncidental> empIncidentalList, SummaryPersonnelCostsTO summaryPersonnelCostsTO) throws HssException, ParseException {
        Map<String, List<TlEmpSalary>> empSalaryMap = GroupingByUtils.getGroupingForMapList(empSalaryList, TlEmpSalary::getEmpdeptid);
        Map<String, List<TlEmpIncidental>> empIncidentalMap = GroupingByUtils.getGroupingForMapList(empIncidentalList, TlEmpIncidental::getEmpdeptid);
        Map<String, SysDepart> departMap = departService.getDepartMap(new QueryWrapper<>());
        List<EmpCostVO> empCostVOList = new ArrayList<>();
        for (String empSalaryDepId : empSalaryMap.keySet()) {
            for (String empIncidentalDepId : empIncidentalMap.keySet()) {
                if (empSalaryDepId.equals(empSalaryDepId) && departMap.containsKey(empSalaryDepId)) {
                    empCostVOList.add(buildEmpCostVO(departMap.get(empSalaryDepId), empSalaryMap.get(empSalaryDepId), empIncidentalMap.get(empIncidentalDepId)));
                    break;
                }
            }
        }
        return empCostVOList;
    }

    /**
     * 人员杂费汇总列表构造
     *
     * @param depart
     * @param empSalaryList
     * @param empIncidentalList
     * @return java.util.List<com.hskn.hss.module.totalcost.vo.EmpCostVO>
     * @author zcy
     * @date 2021/10/26 9:04
     */
    private EmpCostVO buildEmpCostVO(SysDepart depart, List<TlEmpSalary> empSalaryList, List<TlEmpIncidental> empIncidentalList) {
        EmpCostVO empCostVO = new EmpCostVO();
        if (empSalaryList.isEmpty() && empIncidentalList.isEmpty()) {
            return empCostVO;
        }
        BigDecimal allCostSum = BigDecimal.ZERO;
        BigDecimal basicSalary = BigDecimal.ZERO;
        BigDecimal postSalary = BigDecimal.ZERO;
        BigDecimal meritSalary = BigDecimal.ZERO;
        BigDecimal subsidy = BigDecimal.ZERO;
        BigDecimal socialSecurity = BigDecimal.ZERO;
        BigDecimal providentFund = BigDecimal.ZERO;
        BigDecimal incomeTax = BigDecimal.ZERO;
        BigDecimal deduction = BigDecimal.ZERO;
        BigDecimal otherpay = BigDecimal.ZERO;
        for (TlEmpSalary empSalary : empSalaryList) {
            //基本工资
            allCostSum = allCostSum.add(empSalary.getBasicSalary());
            //岗位工资
            allCostSum = allCostSum.add(empSalary.getPostSalary());
            //绩效工资
            allCostSum = allCostSum.add(empSalary.getMeritSalary());
            //补贴
            allCostSum = allCostSum.add(empSalary.getSubsidy());
            //社保
            allCostSum = allCostSum.add(empSalary.getSocialSecurity());
            //公积金
            allCostSum = allCostSum.add(empSalary.getProvidentFund());
            //个税
            allCostSum = allCostSum.subtract(empSalary.getIncomeTax());
            //扣款
            allCostSum = allCostSum.subtract(empSalary.getDeduction());

            //基本工资计算
            basicSalary = basicSalary.add(empSalary.getBasicSalary());
            //岗位工资计算
            postSalary = postSalary.add(empSalary.getPostSalary());
            //绩效工资计算
            meritSalary = meritSalary.add(empSalary.getMeritSalary());
            //补贴计算
            subsidy = subsidy.add(empSalary.getSubsidy());
            //社保计算
            socialSecurity = socialSecurity.add(empSalary.getSocialSecurity());
            //公积金计算
            providentFund = providentFund.add(empSalary.getProvidentFund());
            //个税计算
            incomeTax = incomeTax.add(empSalary.getIncomeTax());
            //扣款计算
            deduction = deduction.add(empSalary.getDeduction());
        }
        for (TlEmpIncidental tlEmpIncidental : empIncidentalList) {
            allCostSum = allCostSum.add(tlEmpIncidental.getPaycost());
            otherpay = otherpay.add(tlEmpIncidental.getPaycost());
        }
        empCostVO.setTotalsalary(allCostSum);
        empCostVO.setBasicsalary(basicSalary);
        empCostVO.setPostsalary(postSalary);
        empCostVO.setMeritsalary(meritSalary);
        empCostVO.setSubsidy(subsidy);
        empCostVO.setSocialsecurity(socialSecurity);
        empCostVO.setProvidentfund(providentFund);
        empCostVO.setIncometax(incomeTax);
        empCostVO.setDeduction(deduction);
        empCostVO.setOtherpay(otherpay);
        empCostVO.setDepName(depart.getDepartName());
        empCostVO.setOrgType(depart.getOrgType());
        String startMonth = "";
        String endMonth = "";
        if ((empSalaryList.size() + empIncidentalList.size()) > 2) {
            if (empSalaryList.get(empSalaryList.size()-1).getMonthDate().getTime() >= empIncidentalList.get(empSalaryList.size()-1).getPaydate().getTime()) {
                startMonth = empSalaryList.get(empSalaryList.size() - 1).getMonth();
            } else {
                startMonth = empIncidentalList.get(empSalaryList.size() - 1).getMonth();
            }
            if (empSalaryList.get(0).getMonthDate().getTime() >= empIncidentalList.get(0).getPaydate().getTime()) {
                endMonth = empSalaryList.get(0).getMonth();
            } else {
                endMonth = empIncidentalList.get(0).getMonth();
            }
            empCostVO.setMonth(startMonth + "-" + endMonth);

        } else {
            if (!empSalaryList.isEmpty()) {
                if (empSalaryList.size() > 1) {
                    empCostVO.setMonth(empSalaryList.get(empSalaryList.size() - 1).getMonth() + "-" + empSalaryList.get(0).getMonth());
                } else {
                    empCostVO.setMonth(empSalaryList.get(0).getMonth());
                }
            }
            if (!empIncidentalList.isEmpty()) {
                if (empIncidentalList.size() > 1) {
                    empCostVO.setMonth(empIncidentalList.get(empSalaryList.size() - 1).getMonth() + "-" + empIncidentalList.get(0).getMonth());
                } else {
                    empCostVO.setMonth(empIncidentalList.get(0).getMonth());
                }
            }
        }
        return empCostVO;
    }

    /**
     * 车辆费用汇总
     *
     * @param summaryVehicleCostTO
     * @return java.util.List<java.util.List < java.lang.Object>>
     * @author zcy
     * @date 2021/10/26 17:02
     */
    @Override
    public List<List<Object>> getcarChars(SummaryVehicleCostDTO summaryVehicleCostDTO, SummaryVehicleCostTO summaryVehicleCostTO) throws ParseException {
        List<BTable> allBTableList = new ArrayList<>();
        List<BTable> oilcostCfgBTableList = new ArrayList<>();
        List<BTable> repairCfgBTableList = new ArrayList<>();
        List<BTable> maintainCfgBTableList = new ArrayList<>();
        List<BTable> insuranceCfgBTableList = new ArrayList<>();
        List<BTable> inspectionCfgBTableList = new ArrayList<>();
        List<BTable> violationCfgBTableList = new ArrayList<>();
        List<BTable> othercarcostCfgBTableList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        List<List<Object>> resultList = new ArrayList<>();
        List<Object> res = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        dateList = Tools.getMonthList(simpleDateFormat, summaryVehicleCostTO.getStartDate(), summaryVehicleCostTO.getEndDate());
        for (String date : dateList) {
            res.add(date);
            resultList.add(res);
            res = new ArrayList<>();
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getOilcostCfgList())) {
            summaryVehicleCostDTO.getOilcostCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlOilcostCfg>> oilcostCfgMap = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getOilcostCfgList(), TlOilcostCfg::getCosttimeString);
            for (String costTime : oilcostCfgMap.keySet()) {
                oilcostCfgBTableList.add(buildCarBTable(costTime, "油费", oilcostCfgMap.get(costTime)));
                allBTableList.add(buildCarBTable(costTime, "油费", oilcostCfgMap.get(costTime)));

            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getRepairCfgList())) {
            summaryVehicleCostDTO.getRepairCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlRepairCfg>> repairCfgMap = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getRepairCfgList(), TlRepairCfg::getCosttimeString);
            for (String costTime : repairCfgMap.keySet()) {
                repairCfgBTableList.add(buildCarBTable(costTime, "维修费", repairCfgMap.get(costTime)));
                allBTableList.add(buildCarBTable(costTime, "维修费", repairCfgMap.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getMaintainCfgList())) {
            summaryVehicleCostDTO.getMaintainCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlMaintainCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getMaintainCfgList(), TlMaintainCfg::getCosttimeString);
            for (String costTime : map.keySet()) {
                maintainCfgBTableList.add(buildCarBTable(costTime, "保养费", map.get(costTime)));
                allBTableList.add(buildCarBTable(costTime, "保养费", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getInsuranceCfgList())) {
            summaryVehicleCostDTO.getInsuranceCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlInsuranceCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getInsuranceCfgList(), TlInsuranceCfg::getCosttimeString);
            for (String costTime : map.keySet()) {
                insuranceCfgBTableList.add(buildCarBTable(costTime, "保险费", map.get(costTime)));
                allBTableList.add(buildCarBTable(costTime, "保险费", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getInspectionCfgList())) {
            summaryVehicleCostDTO.getInspectionCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlInspectionCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getInspectionCfgList(), TlInspectionCfg::getCosttimeString);
            for (String costTime : map.keySet()) {
                inspectionCfgBTableList.add(buildCarBTable(costTime, "年检费", map.get(costTime)));
                allBTableList.add(buildCarBTable(costTime, "年检费", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getViolationCfgList())) {
            summaryVehicleCostDTO.getViolationCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlViolationCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getViolationCfgList(), TlViolationCfg::getCosttimeString);
            for (String costTime : map.keySet()) {
                violationCfgBTableList.add(buildCarBTable(costTime, "违章费", map.get(costTime)));
                allBTableList.add(buildCarBTable(costTime, "违章费", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getOthercarcostCfgList())) {
            summaryVehicleCostDTO.getOthercarcostCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlOthercarcostCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getOthercarcostCfgList(), TlOthercarcostCfg::getCosttimeString);
            for (String costTime : map.keySet()) {
                othercarcostCfgBTableList.add(buildCarBTable(costTime, "车杂费", map.get(costTime)));
                allBTableList.add(buildCarBTable(costTime, "车杂费", map.get(costTime)));
            }
        }
        Map<String, List<BTable>> bTableMap = GroupingByUtils.getGroupingForMapList(allBTableList, BTable::getTime);
        List<BTable> allResultBTableList = new ArrayList<>();
        for (String time : bTableMap.keySet()) {
            allResultBTableList.add(buildAllCarBTable(time, bTableMap.get(time)));
        }
        resultList = Tools.getBTable(resultList, allResultBTableList);

        resultList = Tools.getBTable(resultList, oilcostCfgBTableList);
        resultList = Tools.getBTable(resultList, repairCfgBTableList);

        resultList = Tools.getBTable(resultList, maintainCfgBTableList);

        resultList = Tools.getBTable(resultList, insuranceCfgBTableList);

        resultList = Tools.getBTable(resultList, inspectionCfgBTableList);

        resultList = Tools.getBTable(resultList, violationCfgBTableList);

        resultList = Tools.getBTable(resultList, othercarcostCfgBTableList);
        return resultList;
    }


    /**
     * 全部车辆费用汇总构造
     *
     * @param time
     * @param list
     * @return com.hskn.hss.utils.BTable
     * @author zcy
     * @date 2021/10/26 17:03
     */
    private BTable buildAllCarBTable(String time, List<BTable> list) {
        BTable result = new BTable();
        BigDecimal sum = BigDecimal.ZERO;
        for (BTable bTable : list) {
            sum = sum.add(new BigDecimal(bTable.getData().toString()));
        }
        result.setData(sum);
        result.setTime(time);
        return result;
    }

    /**
     * 各类型车辆费用汇总构造
     *
     * @param time
     * @param typeName
     * @param list
     * @return com.hskn.hss.utils.BTable
     * @author zcy
     * @date 2021/10/26 17:04
     */
    private <T extends totalcost> BTable buildCarBTable(String time, String typeName, List<T> list) {
        BTable bTable = new BTable();
        if (CollectionUtils.isEmpty(list)) {
            return bTable;
        }
        BigDecimal sum = BigDecimal.ZERO;
        for (totalcost tc : list) {
            sum = sum.add(tc.getTotalcost());
        }
        bTable.setTime(time);
        bTable.setCarTypeName(typeName);
        bTable.setData(sum);
        return bTable;
    }

    private <T extends totalcost> BTable buildCarBTable(String name, String time, String typeName, List<T> list) {
        BTable bTable = new BTable();
        if (CollectionUtils.isEmpty(list)) {
            return bTable;
        }
        BigDecimal sum = BigDecimal.ZERO;
        for (totalcost tc : list) {
            sum = sum.add(tc.getTotalcost());
            name += tc.getCosttimeString() + "##";
        }
        bTable.setTime(time);
        if (StringUtils.isNotEmpty(name)) {
            bTable.setName(name.substring(0, name.lastIndexOf("##")));
        } else {
            bTable.setName(name);
        }
        bTable.setCarTypeName(typeName);
        bTable.setData(sum);
        return bTable;
    }

    /**
     * 车辆成本费用汇总查询
     *
     * @param summaryVehicleCostTO
     * @return com.hskn.hss.module.totalcost.dto.SummaryVehicleCostDTO
     * @author zcy
     * @date 2021/10/26 17:05
     */
    @Override
    @DataScope(deptAlias = "deptidforcar", userAlias = "creatBy")
    public SummaryVehicleCostDTO buildSummaryVehicleCostDTO(SummaryVehicleCostTO summaryVehicleCostTO) {
        QueryWrapper<TlOilcostCfg> oilcostCfgQueryWrapper = new QueryWrapper<>();
        QueryWrapper<TlRepairCfg> repairCfgQueryWrapper = new QueryWrapper<>();
        QueryWrapper<TlMaintainCfg> maintainCfgQueryWrapper = new QueryWrapper<>();
        QueryWrapper<TlInsuranceCfg> insuranceCfgQueryWrapper = new QueryWrapper<>();
        QueryWrapper<TlInspectionCfg> inspectionCfgQueryWrapper = new QueryWrapper<>();
        QueryWrapper<TlViolationCfg> violationCfgQueryWrapper = new QueryWrapper<>();
        QueryWrapper<TlOthercarcostCfg> othercarcostCfgQueryWrapper = new QueryWrapper<>();

        if (summaryVehicleCostTO.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(summaryVehicleCostTO.getParams().get("dataScope").toString())) {
            oilcostCfgQueryWrapper.lambda().apply(summaryVehicleCostTO.getParams().get("dataScope").toString());

            repairCfgQueryWrapper.lambda().apply(summaryVehicleCostTO.getParams().get("dataScope").toString());

            maintainCfgQueryWrapper.lambda().apply(summaryVehicleCostTO.getParams().get("dataScope").toString());

            insuranceCfgQueryWrapper.lambda().apply(summaryVehicleCostTO.getParams().get("dataScope").toString());

            inspectionCfgQueryWrapper.lambda().apply(summaryVehicleCostTO.getParams().get("dataScope").toString());

            violationCfgQueryWrapper.lambda().apply(summaryVehicleCostTO.getParams().get("dataScope").toString());

            othercarcostCfgQueryWrapper.lambda().apply(summaryVehicleCostTO.getParams().get("dataScope").toString());

        }
        if (StringUtils.isNotEmpty(summaryVehicleCostTO.getDepTypeName())) {
            QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
            departQueryWrapper.lambda().eq(SysDepart::getOrgType, summaryVehicleCostTO.getDepTypeName());
            Map<String, SysDepart> departMap = departService.getDepartMap(departQueryWrapper);
            oilcostCfgQueryWrapper.lambda().in(TlOilcostCfg::getDeptidforcar, departMap.keySet());
            repairCfgQueryWrapper.lambda().in(TlRepairCfg::getDeptidforcar, departMap.keySet());
            maintainCfgQueryWrapper.lambda().in(TlMaintainCfg::getDeptidforcar, departMap.keySet());
            insuranceCfgQueryWrapper.lambda().in(TlInsuranceCfg::getDeptidforcar, departMap.keySet());
            inspectionCfgQueryWrapper.lambda().in(TlInspectionCfg::getDeptidforcar, departMap.keySet());
            violationCfgQueryWrapper.lambda().in(TlViolationCfg::getDeptidforcar, departMap.keySet());
            othercarcostCfgQueryWrapper.lambda().in(TlOthercarcostCfg::getDeptidforcar, departMap.keySet());
        }
        if (StringUtils.isNotEmpty(summaryVehicleCostTO.getDepId())) {
            oilcostCfgQueryWrapper.lambda().eq(TlOilcostCfg::getDeptidforcar, summaryVehicleCostTO.getDepId());
            repairCfgQueryWrapper.lambda().eq(TlRepairCfg::getDeptidforcar, summaryVehicleCostTO.getDepId());
            maintainCfgQueryWrapper.lambda().eq(TlMaintainCfg::getDeptidforcar, summaryVehicleCostTO.getDepId());
            insuranceCfgQueryWrapper.lambda().eq(TlInsuranceCfg::getDeptidforcar, summaryVehicleCostTO.getDepId());
            inspectionCfgQueryWrapper.lambda().eq(TlInspectionCfg::getDeptidforcar, summaryVehicleCostTO.getDepId());
            violationCfgQueryWrapper.lambda().eq(TlViolationCfg::getDeptidforcar, summaryVehicleCostTO.getDepId());
            othercarcostCfgQueryWrapper.lambda().eq(TlOthercarcostCfg::getDeptidforcar, summaryVehicleCostTO.getDepId());
        }
        if (StringUtils.isNotEmpty(summaryVehicleCostTO.getCarType()) || StringUtils.isNotEmpty(summaryVehicleCostTO.getCarName())) {
            QueryWrapper<TlCar> carQueryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(summaryVehicleCostTO.getCarName())) {
                carQueryWrapper.lambda().eq(TlCar::getCarnumber, summaryVehicleCostTO.getCarName());
            }
            if (StringUtils.isNotEmpty(summaryVehicleCostTO.getCarType())) {
                carQueryWrapper.lambda().eq(TlCar::getCartypeid, summaryVehicleCostTO.getCarType());
            }
            Map<String, TlCar> carMap = carService.getCarMap(carQueryWrapper);
            oilcostCfgQueryWrapper.lambda().in(TlOilcostCfg::getCarnumid, carMap.keySet());
            repairCfgQueryWrapper.lambda().in(TlRepairCfg::getCarnumid, carMap.keySet());
            maintainCfgQueryWrapper.lambda().in(TlMaintainCfg::getCarnumid, carMap.keySet());
            insuranceCfgQueryWrapper.lambda().in(TlInsuranceCfg::getCarnumid, carMap.keySet());
            inspectionCfgQueryWrapper.lambda().in(TlInspectionCfg::getCarnumid, carMap.keySet());
            violationCfgQueryWrapper.lambda().in(TlViolationCfg::getCarnumid, carMap.keySet());
            othercarcostCfgQueryWrapper.lambda().in(TlOthercarcostCfg::getCarnumid, carMap.keySet());
        }
        if (Objects.nonNull(summaryVehicleCostTO.getStartDate()) && Objects.nonNull(summaryVehicleCostTO.getEndDate())) {
            oilcostCfgQueryWrapper.lambda().between(TlOilcostCfg::getCosttime, summaryVehicleCostTO.getStartDate(), summaryVehicleCostTO.getEndDate());
            repairCfgQueryWrapper.lambda().between(TlRepairCfg::getCosttime, summaryVehicleCostTO.getStartDate(), summaryVehicleCostTO.getEndDate());
            maintainCfgQueryWrapper.lambda().between(TlMaintainCfg::getCosttime, summaryVehicleCostTO.getStartDate(), summaryVehicleCostTO.getEndDate());
            insuranceCfgQueryWrapper.lambda().between(TlInsuranceCfg::getCosttime, summaryVehicleCostTO.getStartDate(), summaryVehicleCostTO.getEndDate());
            inspectionCfgQueryWrapper.lambda().between(TlInspectionCfg::getCosttime, summaryVehicleCostTO.getStartDate(), summaryVehicleCostTO.getEndDate());
            violationCfgQueryWrapper.lambda().between(TlViolationCfg::getCosttime, summaryVehicleCostTO.getStartDate(), summaryVehicleCostTO.getEndDate());
            othercarcostCfgQueryWrapper.lambda().between(TlOthercarcostCfg::getCosttime, summaryVehicleCostTO.getStartDate(), summaryVehicleCostTO.getEndDate());
        }
        List<TlOilcostCfg> oilcostCfgList = null;
        if ((StringUtils.isNotEmpty(summaryVehicleCostTO.getCostType()) && "油费".equals(summaryVehicleCostTO.getCostType()) || StringUtils.isEmpty(summaryVehicleCostTO.getCostType()))) {
            oilcostCfgList = oilcostCfgMapper.selectList(oilcostCfgQueryWrapper);
        }

        List<TlRepairCfg> repairCfgList = null;
        if ((StringUtils.isNotEmpty(summaryVehicleCostTO.getCostType()) && "维修费".equals(summaryVehicleCostTO.getCostType()) || StringUtils.isEmpty(summaryVehicleCostTO.getCostType()))) {
            repairCfgList = repairCfgMapper.selectList(repairCfgQueryWrapper);
        }

        List<TlMaintainCfg> maintainCfgList = null;
        if ((StringUtils.isNotEmpty(summaryVehicleCostTO.getCostType()) && "保养费".equals(summaryVehicleCostTO.getCostType())) || StringUtils.isEmpty(summaryVehicleCostTO.getCostType())) {
            maintainCfgList = maintainCfgMapper.selectList(maintainCfgQueryWrapper);
        }

        List<TlInsuranceCfg> insuranceCfgList = null;
        if ((StringUtils.isNotEmpty(summaryVehicleCostTO.getCostType()) && "保险费".equals(summaryVehicleCostTO.getCostType())) || StringUtils.isEmpty(summaryVehicleCostTO.getCostType())) {
            insuranceCfgList = insuranceCfgMapper.selectList(insuranceCfgQueryWrapper);
        }

        List<TlInspectionCfg> inspectionCfgList = null;
        if ((StringUtils.isNotEmpty(summaryVehicleCostTO.getCostType()) && "年检费".equals(summaryVehicleCostTO.getCostType())) || StringUtils.isEmpty(summaryVehicleCostTO.getCostType())) {
            inspectionCfgList = inspectionCfgMapper.selectList(inspectionCfgQueryWrapper);
        }

        List<TlViolationCfg> violationCfgList = null;
        if ((StringUtils.isNotEmpty(summaryVehicleCostTO.getCostType()) && "违章费".equals(summaryVehicleCostTO.getCostType())) || StringUtils.isEmpty(summaryVehicleCostTO.getCostType())) {
            violationCfgList = violationCfgMapper.selectList(violationCfgQueryWrapper);
        }

        List<TlOthercarcostCfg> othercarcostCfgList = null;
        if ((StringUtils.isNotEmpty(summaryVehicleCostTO.getCostType()) && "车杂费".equals(summaryVehicleCostTO.getCostType())) || StringUtils.isEmpty(summaryVehicleCostTO.getCostType())) {
            othercarcostCfgList = othercarcostCfgMapper.selectList(othercarcostCfgQueryWrapper);
        }
        return new SummaryVehicleCostDTO(oilcostCfgList, repairCfgList, maintainCfgList, insuranceCfgList, inspectionCfgList, violationCfgList, othercarcostCfgList);
    }

    /**
     * 车辆费用成本汇总列表
     *
     * @param summaryVehicleCostTO
     * @return java.util.List<com.hskn.hss.module.totalcost.vo.CarCostVO>
     * @author zcy
     * @date 2021/10/26 22:45
     */
    @Override
    public List<CarCostVO> getcarList(SummaryVehicleCostDTO summaryVehicleCostDTO, SummaryVehicleCostTO summaryVehicleCostTO) throws HssException, ParseException {
        List<BTable> allBTableList = new ArrayList<>();
        Map<String, SysDepart> departMap = departService.getDepartMap(new QueryWrapper<>());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getOilcostCfgList())) {
            summaryVehicleCostDTO.getOilcostCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlOilcostCfg>> oilcostCfgMap = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getOilcostCfgList(), TlOilcostCfg::getDeptidforcar);
            for (String depId : oilcostCfgMap.keySet()) {
                allBTableList.add(buildCarBTable("", depId, "油费", oilcostCfgMap.get(depId)));

            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getRepairCfgList())) {
            summaryVehicleCostDTO.getRepairCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlRepairCfg>> repairCfgMap = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getRepairCfgList(), TlRepairCfg::getDeptidforcar);
            for (String depId : repairCfgMap.keySet()) {
                allBTableList.add(buildCarBTable("", depId, "维修费", repairCfgMap.get(depId)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getMaintainCfgList())) {
            summaryVehicleCostDTO.getMaintainCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlMaintainCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getMaintainCfgList(), TlMaintainCfg::getDeptidforcar);
            for (String depId : map.keySet()) {
                allBTableList.add(buildCarBTable("", depId, "保养费", map.get(depId)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getInsuranceCfgList())) {
            summaryVehicleCostDTO.getInsuranceCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlInsuranceCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getInsuranceCfgList(), TlInsuranceCfg::getDeptidforcar);
            for (String depId : map.keySet()) {
                allBTableList.add(buildCarBTable("", depId, "保险费", map.get(depId)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getInspectionCfgList())) {
            summaryVehicleCostDTO.getInspectionCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlInspectionCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getInspectionCfgList(), TlInspectionCfg::getDeptidforcar);
            for (String depId : map.keySet()) {
                allBTableList.add(buildCarBTable("", depId, "年检费", map.get(depId)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getViolationCfgList())) {
            summaryVehicleCostDTO.getViolationCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlViolationCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getViolationCfgList(), TlViolationCfg::getDeptidforcar);
            for (String depId : map.keySet()) {
                allBTableList.add(buildCarBTable("", depId, "违章费", map.get(depId)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getOthercarcostCfgList())) {
            summaryVehicleCostDTO.getOthercarcostCfgList().forEach(x -> x.setCosttimeString(simpleDateFormat.format(x.getCosttime())));
            Map<String, List<TlOthercarcostCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getOthercarcostCfgList(), TlOthercarcostCfg::getDeptidforcar);
            for (String depId : map.keySet()) {
                allBTableList.add(buildCarBTable("", depId, "车杂费", map.get(depId)));
            }
        }
        Map<String, List<BTable>> allBTableMap = GroupingByUtils.getGroupingForMapList(allBTableList, BTable::getTime);
        List<CarCostVO> carCostVOList = new ArrayList<>();
        for (String depId : allBTableMap.keySet()) {
            if (departMap.containsKey(depId)) {
                carCostVOList.add(buildCarCostVO(departMap.get(depId), allBTableMap.get(depId)));
            }
        }
        return carCostVOList;
    }

    /**
     * 车辆费用成本汇总列表构造
     *
     * @param depart
     * @param bTableList
     * @return com.hskn.hss.module.totalcost.vo.CarCostVO
     * @author zcy
     * @date 2021/10/26 22:44
     */
    private CarCostVO buildCarCostVO(SysDepart depart, List<BTable> bTableList) {
        BigDecimal totalCost = BigDecimal.ZERO;
        //油费
        BigDecimal oilCost = BigDecimal.ZERO;
        //修理费
        BigDecimal repairCost = BigDecimal.ZERO;
        //保养
        BigDecimal maintainCost = BigDecimal.ZERO;
        //年检
        BigDecimal inspectionCost = BigDecimal.ZERO;
        //保险
        BigDecimal insuranceCost = BigDecimal.ZERO;
        //违章
        BigDecimal violationCost = BigDecimal.ZERO;
        //车杂
        BigDecimal otherFees = BigDecimal.ZERO;
        String name = "";
        for (BTable bTable : bTableList) {
            name += bTable.getName() + "##";
            totalCost = totalCost.add(new BigDecimal(bTable.getData().toString()));
            if (StringUtils.isNotEmpty(bTable.getCarTypeName()) && bTable.getCarTypeName().equals("油费")) {
                oilCost = oilCost.add(new BigDecimal(bTable.getData().toString()));
            }
            if (StringUtils.isNotEmpty(bTable.getCarTypeName()) && bTable.getCarTypeName().equals("维修费")) {
                repairCost = repairCost.add(new BigDecimal(bTable.getData().toString()));
            }
            if (StringUtils.isNotEmpty(bTable.getCarTypeName()) && bTable.getCarTypeName().equals("保养费")) {
                maintainCost = maintainCost.add(new BigDecimal(bTable.getData().toString()));
            }
            if (StringUtils.isNotEmpty(bTable.getCarTypeName()) && bTable.getCarTypeName().equals("保险费")) {
                insuranceCost = insuranceCost.add(new BigDecimal(bTable.getData().toString()));
            }
            if (StringUtils.isNotEmpty(bTable.getCarTypeName()) && bTable.getCarTypeName().equals("年检费")) {
                inspectionCost = inspectionCost.add(new BigDecimal(bTable.getData().toString()));
            }
            if (StringUtils.isNotEmpty(bTable.getCarTypeName()) && bTable.getCarTypeName().equals("违章费")) {
                violationCost = violationCost.add(new BigDecimal(bTable.getData().toString()));
            }
            if (StringUtils.isNotEmpty(bTable.getCarTypeName()) && bTable.getCarTypeName().equals("车杂费")) {
                otherFees = otherFees.add(new BigDecimal(bTable.getData().toString()));
            }
        }
        name = name.substring(0, name.lastIndexOf("##"));
        List<String> monthList = Arrays.asList(name.split("##"));
        monthList = monthList.stream().sorted().collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(monthList) && monthList.size() > 1) {
            name = monthList.get(0) + "-" + monthList.get(monthList.size() - 1);
        } else if (CollectionUtils.isNotEmpty(monthList)) {
            name = monthList.get(0);
        }
        return new CarCostVO(depart.getDepartName(), depart.getOrgType(), name, totalCost, oilCost, repairCost, maintainCost, inspectionCost, insuranceCost,
                violationCost, otherFees);

    }

    /**
     * 总费用汇总列表
     *
     * @param costOperateList
     * @param empSalaryList
     * @param empIncidentalList
     * @param summaryVehicleCostDTO
     * @return java.util.List<com.hskn.hss.module.totalcost.vo.SummaryTotalCostVO>
     * @author zcy
     * @date 2021/10/27 14:16
     */
    @Override
    public List<SummaryTotalCostVO> gettotalList(SimpleDateFormat formatter, List<TlCostOperate> costOperateList, List<TlEmpSalary> empSalaryList,
                                                 List<TlEmpIncidental> empIncidentalList, SummaryVehicleCostDTO summaryVehicleCostDTO, SummaryTotalCostTO summaryTotalCostTO) throws HssException, ParseException {
        List<SummaryTotalCostVO> summaryTotalCostVOList = new ArrayList<>();
        Map<String, List<TlCostOperate>> costOperateMap = GroupingByUtils.getGroupingForMapList(costOperateList, TlCostOperate::getWaterdept);
        List<BTable> bTableList = new ArrayList<>();
        for (String s : costOperateMap.keySet()) {
            bTableList.add(buildCostOperateBTable(s, "运营", costOperateMap.get(s)));
        }
        Map<String, List<TlEmpSalary>> empSalaryMap = GroupingByUtils.getGroupingForMapList(empSalaryList, TlEmpSalary::getEmpdeptid);
        Map<String, List<TlEmpIncidental>> empIncidentalMap = GroupingByUtils.getGroupingForMapList(empIncidentalList, TlEmpIncidental::getEmpdeptid);
        for (String empSalaryDepId : empSalaryMap.keySet()) {
            for (String empIncidentalDepId : empIncidentalMap.keySet()) {
                if (empSalaryDepId.equals(empIncidentalDepId)) {
                    bTableList.add(buildEmpCostBTable(empSalaryDepId, "人员", empSalaryMap.get(empSalaryDepId), empIncidentalMap.get(empIncidentalDepId)));
                    break;
                }
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getOilcostCfgList())) {
            summaryVehicleCostDTO.getOilcostCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlOilcostCfg>> oilcostCfgMap = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getOilcostCfgList(), TlOilcostCfg::getDeptidforcar);
            for (String costTime : oilcostCfgMap.keySet()) {
                bTableList.add(buildCarBTable("", costTime, "车辆", oilcostCfgMap.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getRepairCfgList())) {
            summaryVehicleCostDTO.getRepairCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlRepairCfg>> repairCfgMap = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getRepairCfgList(), TlRepairCfg::getDeptidforcar);
            for (String costTime : repairCfgMap.keySet()) {
                bTableList.add(buildCarBTable("", costTime, "车辆", repairCfgMap.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getMaintainCfgList())) {
            summaryVehicleCostDTO.getMaintainCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlMaintainCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getMaintainCfgList(), TlMaintainCfg::getDeptidforcar);
            for (String costTime : map.keySet()) {
                bTableList.add(buildCarBTable("", costTime, "车辆", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getInsuranceCfgList())) {
            summaryVehicleCostDTO.getInsuranceCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlInsuranceCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getInsuranceCfgList(), TlInsuranceCfg::getDeptidforcar);
            for (String costTime : map.keySet()) {
                bTableList.add(buildCarBTable("", costTime, "车辆", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getInspectionCfgList())) {
            summaryVehicleCostDTO.getInspectionCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlInspectionCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getInspectionCfgList(), TlInspectionCfg::getDeptidforcar);
            for (String costTime : map.keySet()) {
                bTableList.add(buildCarBTable("", costTime, "车辆", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getViolationCfgList())) {
            summaryVehicleCostDTO.getViolationCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlViolationCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getViolationCfgList(), TlViolationCfg::getDeptidforcar);
            for (String costTime : map.keySet()) {
                bTableList.add(buildCarBTable("", costTime, "车辆", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getOthercarcostCfgList())) {
            summaryVehicleCostDTO.getOthercarcostCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlOthercarcostCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getOthercarcostCfgList(), TlOthercarcostCfg::getDeptidforcar);
            for (String costTime : map.keySet()) {
                bTableList.add(buildCarBTable("", costTime, "车辆", map.get(costTime)));
            }
        }
        Map<String, SysDepart> departMap = departService.getDepartMap(new QueryWrapper<>());
        Map<String, List<BTable>> resultAllBTableMap = GroupingByUtils.getGroupingForMapList(bTableList, BTable::getTime);
        for (String depId : resultAllBTableMap.keySet()) {
            if (departMap.containsKey(depId)) {
                summaryTotalCostVOList.add(buildSummaryTotalCostVO(departMap.get(depId), resultAllBTableMap.get(depId)));
            }
        }
        return summaryTotalCostVOList;
    }

    /**
     * 总费用汇总列表构造
     *
     * @param depart
     * @param bTableList
     * @return com.hskn.hss.module.totalcost.vo.SummaryTotalCostVO
     * @author zcy
     * @date 2021/10/27 14:15
     */
    private SummaryTotalCostVO buildSummaryTotalCostVO(SysDepart depart, List<BTable> bTableList) {
        SummaryTotalCostVO summaryTotalCostVO = new SummaryTotalCostVO();
        BigDecimal cost = BigDecimal.ZERO;
        BigDecimal carCost = BigDecimal.ZERO;
        BigDecimal empCost = BigDecimal.ZERO;
        BigDecimal opeCost = BigDecimal.ZERO;
        String month = "";
        for (BTable bTable : bTableList) {
            cost = cost.add(new BigDecimal(bTable.getData().toString()));
            if (StringUtils.isNotEmpty(bTable.getCarTypeName()) && bTable.getCarTypeName().equals("车辆")) {
                carCost = carCost.add(new BigDecimal(bTable.getData().toString()));
            }
            if (StringUtils.isNotEmpty(bTable.getCarTypeName()) && bTable.getCarTypeName().equals("人员")) {
                empCost = empCost.add(new BigDecimal(bTable.getData().toString()));
            }
            if (StringUtils.isNotEmpty(bTable.getCarTypeName()) && bTable.getCarTypeName().equals("运营")) {
                opeCost = opeCost.add(new BigDecimal(bTable.getData().toString()));
            }
            month += bTable.getName() + "##";
        }
        List<String> monthList = Arrays.asList(month.split("##"));
        monthList = monthList.stream().sorted().collect(Collectors.toList());

        if (monthList.size() > 1) {
            summaryTotalCostVO.setMonth(monthList.get(0) + "-" + monthList.get(monthList.size() - 1));
        } else {
            summaryTotalCostVO.setMonth(monthList.get(0));
        }
        summaryTotalCostVO.setDepartName(depart.getDepartName());
        summaryTotalCostVO.setOrgType(depart.getOrgType());
        summaryTotalCostVO.setCost(cost);
        summaryTotalCostVO.setCarCost(carCost);
        summaryTotalCostVO.setEmpCost(empCost);
        summaryTotalCostVO.setOpeCost(opeCost);
        return summaryTotalCostVO;
    }

    /**
     * 总成本汇总图
     *
     * @param summaryTotalCostTO
     * @return java.util.List<java.util.List < java.lang.Object>>
     * @author zcy
     * @date 2021/10/27 12:18
     */
    @Override
    public List<List<Object>> gettotalChars(SimpleDateFormat formatter, List<TlCostOperate> costOperateList, List<TlEmpSalary> empSalaryList, List<TlEmpIncidental> empIncidentalList, SummaryVehicleCostDTO summaryVehicleCostDTO, SummaryTotalCostTO summaryTotalCostTO) throws ParseException {
        List<List<Object>> resultList = new ArrayList<>();
        List<Object> res = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        dateList = Tools.getMonthList(formatter, summaryTotalCostTO.getStartDate(), summaryTotalCostTO.getEndDate());
        for (String date : dateList) {
            res.add(date);
            resultList.add(res);
            res = new ArrayList<>();
        }
        SummaryOperatingExpensesTO summaryOperatingExpensesTO = JSONObject.parseObject(JSON.toJSONString(summaryTotalCostTO), SummaryOperatingExpensesTO.class);
        Map<String, List<TlCostOperate>> costOperateMap = GroupingByUtils.getGroupingForMapList(costOperateList, TlCostOperate::getPayTimeString);
        List<BTable> bTableList = new ArrayList<>();
        List<BTable> operatingBTableList = new ArrayList<>();
        for (String s : costOperateMap.keySet()) {
            bTableList.add(buildCostOperateBTable(s, costOperateMap.get(s)));
            operatingBTableList.add(buildCostOperateBTable(s, costOperateMap.get(s)));
        }
        SummaryPersonnelCostsTO summaryPersonnelCostsTO = JSONObject.parseObject(JSON.toJSONString(summaryTotalCostTO), SummaryPersonnelCostsTO.class);
        Map<String, List<TlEmpSalary>> empSalaryMap = GroupingByUtils.getGroupingForMapList(empSalaryList, TlEmpSalary::getMonth);
        Map<String, List<TlEmpIncidental>> empIncidentalMap = GroupingByUtils.getGroupingForMapList(empIncidentalList, TlEmpIncidental::getMonth);
        List<BTable> empBTableList = new ArrayList<>();
        List<BTable> empSalaryBTableList = new ArrayList<>();
        List<BTable> empIncidentalBTableList = new ArrayList<>();
        for (String empSalaryMonth : empSalaryMap.keySet()) {
            bTableList.add(buildEmpCostBTable(empSalaryMonth, empSalaryMap.get(empSalaryMonth)));
            empSalaryBTableList.add(buildEmpCostBTable(empSalaryMonth, empSalaryMap.get(empSalaryMonth)));
        }
        for (String empIncidentalMonth : empIncidentalMap.keySet()) {
            bTableList.add(buildEmpCostBTable1(empIncidentalMonth, empIncidentalMap.get(empIncidentalMonth)));
            empIncidentalBTableList.add(buildEmpCostBTable1(empIncidentalMonth, empIncidentalMap.get(empIncidentalMonth)));
        }
        empBTableList.addAll(empSalaryBTableList);
        empBTableList.addAll(empIncidentalBTableList);
        SummaryVehicleCostTO summaryVehicleCostTO = JSONObject.parseObject(JSON.toJSONString(summaryTotalCostTO), SummaryVehicleCostTO.class);
        List<BTable> carBTableList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getOilcostCfgList())) {
            summaryVehicleCostDTO.getOilcostCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlOilcostCfg>> oilcostCfgMap = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getOilcostCfgList(), TlOilcostCfg::getCosttimeString);
            for (String costTime : oilcostCfgMap.keySet()) {
                carBTableList.add(buildCarBTable(costTime, "油费", oilcostCfgMap.get(costTime)));
                bTableList.add(buildCarBTable(costTime, "油费", oilcostCfgMap.get(costTime)));

            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getRepairCfgList())) {
            summaryVehicleCostDTO.getRepairCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlRepairCfg>> repairCfgMap = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getRepairCfgList(), TlRepairCfg::getCosttimeString);
            for (String costTime : repairCfgMap.keySet()) {
                carBTableList.add(buildCarBTable(costTime, "维修费", repairCfgMap.get(costTime)));
                bTableList.add(buildCarBTable(costTime, "维修费", repairCfgMap.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getMaintainCfgList())) {
            summaryVehicleCostDTO.getMaintainCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlMaintainCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getMaintainCfgList(), TlMaintainCfg::getCosttimeString);
            for (String costTime : map.keySet()) {
                carBTableList.add(buildCarBTable(costTime, "保养费", map.get(costTime)));
                bTableList.add(buildCarBTable(costTime, "保养费", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getInsuranceCfgList())) {
            summaryVehicleCostDTO.getInsuranceCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlInsuranceCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getInsuranceCfgList(), TlInsuranceCfg::getCosttimeString);
            for (String costTime : map.keySet()) {
                carBTableList.add(buildCarBTable(costTime, "保险费", map.get(costTime)));
                bTableList.add(buildCarBTable(costTime, "保险费", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getInspectionCfgList())) {
            summaryVehicleCostDTO.getInspectionCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlInspectionCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getInspectionCfgList(), TlInspectionCfg::getCosttimeString);
            for (String costTime : map.keySet()) {
                carBTableList.add(buildCarBTable(costTime, "年检费", map.get(costTime)));
                bTableList.add(buildCarBTable(costTime, "年检费", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getViolationCfgList())) {
            summaryVehicleCostDTO.getViolationCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlViolationCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getViolationCfgList(), TlViolationCfg::getCosttimeString);
            for (String costTime : map.keySet()) {
                carBTableList.add(buildCarBTable(costTime, "违章费", map.get(costTime)));
                bTableList.add(buildCarBTable(costTime, "违章费", map.get(costTime)));
            }
        }
        if (CollectionUtils.isNotEmpty(summaryVehicleCostDTO.getOthercarcostCfgList())) {
            summaryVehicleCostDTO.getOthercarcostCfgList().forEach(x -> x.setCosttimeString(formatter.format(x.getCosttime())));
            Map<String, List<TlOthercarcostCfg>> map = GroupingByUtils.getGroupingForMapList(summaryVehicleCostDTO.getOthercarcostCfgList(), TlOthercarcostCfg::getCosttimeString);
            for (String costTime : map.keySet()) {
                carBTableList.add(buildCarBTable(costTime, "车杂费", map.get(costTime)));
                bTableList.add(buildCarBTable(costTime, "车杂费", map.get(costTime)));
            }
        }
        Map<String, List<BTable>> empBTableMap = GroupingByUtils.getGroupingForMapList(empBTableList, BTable::getTime);
        List<BTable> empResultBTableList = new ArrayList<>();
        for (String time : empBTableMap.keySet()) {
            empResultBTableList.add(buildAllCarBTable(time, empBTableMap.get(time)));
        }
        Map<String, List<BTable>> carBTableMap = GroupingByUtils.getGroupingForMapList(carBTableList, BTable::getTime);
        List<BTable> carResultBTableList = new ArrayList<>();
        for (String time : carBTableMap.keySet()) {
            carResultBTableList.add(buildAllCarBTable(time, carBTableMap.get(time)));
        }
        Map<String, List<BTable>> allBTableMap = GroupingByUtils.getGroupingForMapList(bTableList, BTable::getTime);
        List<BTable> allResultBTableList = new ArrayList<>();
        for (String time : allBTableMap.keySet()) {
            allResultBTableList.add(buildAllCarBTable(time, allBTableMap.get(time)));
        }
        resultList = Tools.getBTable(resultList, allResultBTableList);
        resultList = Tools.getBTable(resultList, carResultBTableList);
        resultList = Tools.getBTable(resultList, empResultBTableList);
        resultList = Tools.getBTable(resultList, operatingBTableList);
        return resultList;
    }

}
